﻿using NCRE学生考试端V1._0.判分操作类;
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Model;
using BLL;
using System.Data;

namespace NCRE学生考试端V1._0.悬浮框
{
    public partial class frmxuanfukuang : Form
    {
        //李芬
        public static Form frmxuanfuk;
        public frmxuanfukuang()
        {
            frmxuanfuk = this;
            InitializeComponent();
        }

        private void frmxuanfukuang_Load(object sender, EventArgs e)
        {
            //查询考试时间-谷海涛-2019年1月2日20:03:37
            DataTable dt = new LogBll().QuerySetUpBll();
            if (dt.Rows.Count != 0)
            {
                minuestext.Text = string.Format(dt.Rows[0]["examTime"].ToString());
            }
            string path = string.Format("D:\\" + MyInfo.MystudentID() + "\\" + "计算机一级考生文件\\");
            btnOpenFile.Text = path;
            timer1.Enabled = true;
            MaximizeBox = false;
            MinimizeBox = false;
            btnLock.Text = "隐藏此提示";
            showForm();
            if (FrmMain.frmmain == null)
            {
                frmxuanfuk.Visible = true;
                btnOpenFile.Visible = true;
            }

            //窗体加载时,提建议模块默认不显示
            btnSub.Visible = false;
            txtQuestion.Visible = false;
            frmxuanfuk.Height += -54;
        }
        /// <summary>
        /// 考试到倒计时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //初始化变量为0
            int second = 0;
            //计算考试剩余时间(/s)
            second = int.Parse(minuestext.Text) * 60 + int.Parse(secondtext.Text);
            //悬浮框显示形式XX分XX秒
            if (second > 1)
            {
                second = second - 1;
                minuestext.Text = (second / 60).ToString();
                //符号/表示取整数
                secondtext.Text = (second % 60).ToString();
                //符号%表示取余数
                if (second == 600)
                {
                    MessageBox.Show("温馨提醒：距离考试结束还有10分钟，请保存自己答题记录(word、Excel、ppt)！");
                    //锁定悬浮框
                    showForm();
                    btnLock.Visible = false;
                }
            }
            else//考试结束
            {
                timer1.Enabled = false;
                frmxuanfuk.Close();
                Close();

                //需要对交卷的窗体进行判断-李芬-2015年12月14日20:18:42
                if (frmMain2.frmmain2 == null || frmMain2.frmmain2.IsDisposed)
                {
                    MessageBox.Show(this, "请先返回到操作题界面然后进行交卷！", "提示", MessageBoxButtons.OK);
                    FrmMain.frmmain.Hide();
                    frmMain2.frmmain2.Show();
                }
                else
                {
                    frmxuanfuk.Close();
                    frmMain2.frmmain2.Hide();
                    FrmJudge frmjudge = new FrmJudge();
                    frmjudge.Show();
                }
            }
        }
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            //打开考生目录
            string path = string.Format("D:\\" + MyInfo.MystudentID() + "\\计算机一级考生文件\\" + MyInfo.MyPaperType() + "卷\\");
            System.Diagnostics.Process.Start("explorer.exe", path);
        }
        #region 交卷按钮  李少然  2015年12月14日15:19:56
        /// <summary>
        /// 交卷按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void jiaojuanbutton3_Click(object sender, EventArgs e)
        {
            jiaojuanbutton3.Enabled = false;

            //先判断是不是重复交卷，如果是就返回，如果不是就进入判分窗体
            ////获取学号                   
            //string studentID = MyInfo.MystudentID()
            ////获取日期
            //DateTime time
            //time = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")
            //声明总分操作B层的类，进行查询总分操作
            //string TimeTamp = MyInfo.TimeTamp()
            // TypeSumFrationBLL typesumfration = new TypeSumFrationBLL()
            // DataTable dt = typesumfration.studentIDscore(studentID, TimeTamp)
            //判断该学生今天之内是否已经判分
            //ifdt.Columns.Count > 1
            //MessageBox.Show(this, "该学生所做的试卷在今天已经判分，不可以再次判分", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            DialogResult dr = MessageBox.Show(this, "请先关闭计算机一级考生文件，再进行交卷！您确定要进行交卷吗？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            //如果选择取消，窗体没有变化，点击确定，就show出判分窗体
            if (dr == DialogResult.Cancel)
            {
                jiaojuanbutton3.Enabled = true;
                return;
            }
            else if (dr == DialogResult.OK)
            {

                //需要对交卷的窗体进行判断-李芬-2015年12月14日20:18:42
                if (frmMain2.frmmain2 == null)
                {
                    MessageBox.Show(this, "请先返回到操作题界面然后进行交卷！", "提示", MessageBoxButtons.OK);
                }
                else
                {
                    frmxuanfuk.Close();
                    frmMain2.frmmain2.Hide();
                    FrmJudge frmjudge = new FrmJudge();
                    frmjudge.Show();
                }
            }
        }
        #endregion

        #region 设置鼠标拖动悬浮框-李芬-2015年12月15日09:59:57
        /// <summary>
        ///记录鼠标指针的坐标  
        /// </summary>
        private Point mouseOffset;
        /// <summary>
        /// 记录鼠标按键是否按下
        /// </summary>
        private bool isMouseDown = false;
        //private bool isBoundary = false;   //是否在边界上
        private void frmxuanfukuang_MouseDown_1(object sender, MouseEventArgs e)
        {
            int xOffset;
            //int yOffset
            if (e.Button == MouseButtons.Left)
            {
                xOffset = -e.X - SystemInformation.FrameBorderSize.Width;
                //yOffset = -e.Y - SystemInformation.CaptionHeight - SystemInformation.FrameBorderSize.Height
                mouseOffset = new Point(xOffset, 0);
                //this.MouseMove += frmxuanfukuang_MouseMove
                //isBoundary = false
                isMouseDown = true;
            }
        }

        private void frmxuanfukuang_MouseMove_1(object sender, MouseEventArgs e)
        {
            Rectangle ScreenArea = Screen.GetWorkingArea(this);
            int screenWidth = ScreenArea.Width; //屏幕宽度 
            Point mousePos = MousePosition;
            mousePos.Offset(mouseOffset.X, 0);
            if (Location.X < 0)
            {
                Location = new Point(10, Location.Y);
                isMouseDown = false;
            }
            if (Location.X + Width > screenWidth)
            {
                Location = new Point(screenWidth - 10 - Width, Location.Y);
                isMouseDown = false;
            }
            if (isMouseDown)
            {
                mousePos.Y = 0;
                Location = mousePos;
            }
        }

        private void frmxuanfukuang_MouseUp_1(object sender, MouseEventArgs e)
        {
            // 修改鼠标状态isMouseDown的值   
            // 确保只有鼠标左键按下并移动时，才移动窗体   
            if (e.Button == MouseButtons.Left)
            {
                //this.MouseMove -= frmxuanfukuang_MouseMove
                isMouseDown = false;
            }
        }
        //设置窗体显示状态 
        [DllImport("user32.dll")]
        private static extern int SetWindowPos(IntPtr hwnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
        #endregion

        private void btnLock_Click(object sender, EventArgs e)
        {
            if (btnLock.Text == "隐藏此提示")
            {
                btnLock.Text = "锁定此提示";
                hideForm();
                //自动隐藏
            }
            else
            {
                btnLock.Text = "隐藏此提示";
                showForm();
            }
        }
        /// <summary>
        /// 隐藏窗体
        /// </summary>
        void hideForm()
        {
            frmxuanfuk.Top = -50;
            //frmxuanfuk.Left = Left = Screen.PrimaryScreen.Bounds.Width - 960;
            frmxuanfuk.Left = Screen.PrimaryScreen.Bounds.Width - 960;
        }
        /// <summary>
        /// 显示窗体
        /// </summary>
        void showForm()
        {
            frmxuanfuk.Top = 0;
            frmxuanfuk.Left = Screen.PrimaryScreen.Bounds.Width - 960;
        }

        private void frmxuanfukuang_MouseHover(object sender, EventArgs e)
        {
            showForm();
        }

        private void frmxuanfukuang_MouseLeave(object sender, EventArgs e)
        {
            //鼠标离开悬浮框边界触发
            if (MousePosition.X < Left || MousePosition.X > Left + Size.Width || MousePosition.Y >Size.Height)
            {
                //考试倒计时10分钟锁定+隐藏按钮触发
                if (btnLock.Text == "锁定此提示" && int.Parse(minuestext.Text) * 60 + int.Parse(secondtext.Text) > 600)
                {
                    hideForm();
                }
            }
        }

        #region 补充：提交建议的模块-郑璐璐-2018年12月7日10:39:33
        //提交建议
        private void btnSub_Click(object sender, EventArgs e)
        {
           
                StudentInfoEntity studentinfo = new StudentInfoEntity();
                //LoginEntity.studentID为登录窗体传过来的全局变量
                studentinfo.studentID = LoginEntity.studentID;
                StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL();
                DataTable dt = studentinfobll.SelectStudentInfoByID(studentinfo);


                QuestionEntity question = new QuestionEntity();
                question.Question = txtQuestion.Text.ToString();
                question.Studentid = dt.Rows[0]["studentID"].ToString();
                TestQuestionBLL TQuestion = new TestQuestionBLL();


                //如果文本框内的内容不为空，也不是默认值，那么就进行提交操作
                if (txtQuestion.Text != "" && txtQuestion.Text != "请在此处提建议,期待给您提供更好的服务.点击提交按钮,即可提交您的建议")
                {
                    int result = TQuestion.InsertQuestion(question);
                    if (result > 0)
                    {
                        MessageBox.Show("谢谢您宝贵的建议！");
                        txtQuestion.Text = "请在此处提建议,期待给您提供更好的服务.点击提交按钮,即可提交您的建议";
                    }
                }
            //不管有没有提交建议,只要点击"提交"按钮,即将窗体恢复为原来的样子:
            btnSub.Visible = false;
            txtQuestion.Visible = false;
            frmxuanfuk.Height += -54;
            //frmxuanfuk.Width += -107;
           
            //文本框为空操作或等于默认值时为空操作



        }

        //txtQuestion文本框显示默认值：请在此处提建议,期待给您提供更好的服务.点击提交按钮,即可提交您的建议
        //使用MouseLeave事件时,需要在Designer里面添加相关委托
        private void txtQuestion_MouseLeave(object sender, EventArgs e)
        {
            //判断建议文本框是否为空，若不为空则鼠标移动出控件范围后，内容还是输入的内容
            if (txtQuestion.Text != "")
            {
                string str = txtQuestion.Text;
                txtQuestion.Text = str;
            }
            //如果文本框没有输入值，则鼠标移出控件范围后，内容变为默认内容
            else
            {
                txtQuestion.Text = "请在此处提建议,期待给您提供更好的服务.点击提交按钮,即可提交您的建议";
            }
        }

        
       


        //限制文本框：不能输入非法字符
        private void txtQuestion_KeyPress(object sender, KeyPressEventArgs e)
        {

            //只能输入数字0-9，字母A-Z，a-z，不能输入汉字
            //if (e.KeyChar <= '9' && e.KeyChar >= '0' || e.KeyChar <= 'Z' && e.KeyChar >= 'A' || e.KeyChar <= 'z' && e.KeyChar >= 'a')
            //{
            //    e.Handled = false;
            //}
            //else
            //{
            //    e.Handled = true;
            //}


            //if (e.KeyChar >= 33 && e.KeyChar <= 46 || e.KeyChar >= 58 && e.KeyChar <= 64 )
            //{
            //    e.Handled = true;
            //}
            //else
            //{
            //    e.Handled = true;
            //}

            //写一个方法,直接调用即可
            check(e);



        }
        public void check(KeyPressEventArgs e)
        {
            char[] charstr = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '（', '）', '<', '>', '?', ':', '"', '{', '}', '~', '-', '[', ']', '|' };// 非法字符数组（有几个就写几个用单引号引上‘’用，点隔开）
            for (int i = 0; i < charstr.Length; i++)
            {
                if (e.KeyChar == charstr[i])
                    e.Handled = true;
            }
        }

        private void btnAdvice_Click(object sender, EventArgs e)
        {
            //当点击"提建议"按钮时,代码只执行一次
            if (btnAdvice.Tag ==null )
            {
                btnAdvice.Tag = true;
                //点击"提建议"按钮时,显示相关模块
                btnSub.Visible = true;
                btnAdvice.Visible = false;
                txtQuestion.Visible = true;
                frmxuanfuk.Height += 54;
                frmxuanfuk.Width += -107;
            }           
        }
        #endregion

        //txtQuestion控件点击时，txtQuestion文本框内容为空
        private void txtQuestion_Click(object sender, EventArgs e)
        {
            //如果txtQuestion中的内容为默认值，则再次点击之后清空内容
            if (txtQuestion.Text == "请在此处提建议,期待给您提供更好的服务.点击提交按钮,即可提交您的建议")
            {
                txtQuestion.Text = "";
            }
            //如果不是默认值则是已经输入的内容
            else
            {
                string str = txtQuestion.Text;
                txtQuestion.Text = str;
            }
        }
}
}
